<?php
!defined('BASE_PATH') && die('forbidden');


if(!class_exists('core_db_mysql',false)){
	include(core::parse_class_path('core_db_mysql'));
}


class core_db_comm extends core_db_mysql {
	
	protected static $result;
	
	public static function query($sql, $m_or_s = NULL){
		
		self::$result = parent::query($sql, $m_or_s);
		
		return self::$result;
		
	}
	
	public static function fetch_array(){
		
		return parent::fetch_array(self::$result);
		
	}
	
	public static function fetch_row(){
		
		return parent::fetch_row(self::$result);
		
	}

	public static function num_rows() {
		
		return parent::num_rows(self::$result);
		
	}
	
	public static function free_result(){
		
		return parent::free_result(self::$result);
		
	}
	
	public static function get($fields,$table,$where = '',$groupBy = '',$orderBy = '',$limit = '50'){
		
		'where' != strtolower(substr($where,0,5)) && $where && $where = 'WHERE '.$where;
		'group' != strtolower(substr($groupBy,0,5)) && $groupBy && $groupBy = 'GROUP BY '.$groupBy;
		'order' != strtolower(substr($orderBy,0,5)) && $orderBy && $orderBy = 'ORDER BY '.$orderBy;
		'limit' != strtolower(substr($limit,0,5)) && $limit != '' && $limit = 'LIMIT '.$limit;
		
		return self::query("SELECT $fields FROM $table $where $groupBy $orderBy $limit");
	}
	
	public static function getpage($fields,$table,$where = '',$groupBy = '',$orderBy = '',$page = 1,$pageSize = 20,$count = -1) {
		
		'group' != strtolower(substr($groupBy,0,5)) && $groupBy && $groupBy = 'GROUP BY '.$groupBy;
		if($count<0){
			$_count = $groupBy ? 'COUNT(DISTINCT '.trim(substr($groupBy,9)).')' : 'COUNT(*)';
			self::get($_count.' as num',$table,$where,'','',1);
			if ($row = self::fetch_array()) {
				$count = $row['num'];
			}
		}
		if ($count<1) {
			return array();
		}
		$page<1 && $page = 1;
		$limit = ($page-1)*$pageSize . ',' . $pageSize;
		
		self::get($fields,$table,$where,$groupBy,$orderBy,$limit);
		$rs = array();
		while ($row = self::fetch_array()) {
			$rs[] = $row;
		}
		return array('rs' => $rs,'count' => $count);
		
	}
	
	function insert($table,$data,$ignore = 1){
		$ignore = $ignore? 'IGNORE' : '';
		$sql="INSERT $ignore INTO $table (".implode(',',array_keys($data)).") VALUES ('".implode('\',\'',$data)."')";
		$result = self::query($sql);
		return $result === false ? false : self::affected_rows();
	}
	
	function update($table,$data,$where,$limit=''){
		if ($where && strtolower(substr($where,0,5)) != 'where') {
			$where='WHERE '.$where;
		}else {
			$where='';
		}
		
		if ($limit && strtolower(substr($limit,0,5)) != 'limit') {
			$limit = 'LIMIT '.$limit;
		}else {
			$limit = '';
		}
		
		foreach ($data as $k => $v) {
			$data[$k] = "$k='$v'";
		}
		$result = self::query("UPDATE $table SET ".implode(',',$data)." $where $limit");
		return $result === false ? false : self::affected_rows();
	}
	
	function delete($table,$where,$limit = ''){
		if ($where && strtolower(substr($where,0,5)) != 'where') {
			$where='WHERE '.$where;
		}else {
			$where = '';
		}
		
		if ($limit && strtolower(substr($limit,0,5)) != 'limit') {
			$limit = 'LIMIT '.$limit;
		}else {
			$limit = '';
		}
		
		$result = self::query("DELETE FROM $table $where $limit");
		return $result === false ? false : self::affected_rows();
	}
	
}